package com.yqg.education.service.impl;

import com.yqg.education.entity.Course;
import com.yqg.education.mapper.*;
import com.yqg.education.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yqg.education.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CourseServiceImpl 服务实现类
 * @author 张芸博
 * @since 2025-04-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Resource
    private CourseMapper courseMapper;
    @Resource
    private IUserService userService;

    @Resource
    private TrainClassMapper trainClassMapper;

    @Resource
    private UserMapper userMapper;
    @Resource
    private LiveMapper liveMapper;
    @Resource
    private PeriodMapper periodMapper;
    @Resource
    private LivePlaybackMapper livePlaybackMapper;

    public Map<String, Object> findStuPage(Integer pageNum, Integer pageSize, String name, Integer typeId, Integer userId) {
        Integer pageStartNumber = pageNum * pageSize - pageSize;
        List<Long> courseId = this.userService.getCourseIdByUserId(userId);

        List<Course> courseList = courseMapper.findStuPage(pageStartNumber, pageSize, name, typeId, courseId);
        Long count = courseMapper.findStuCount(name, typeId, courseId);
        Map<String, Object> courseMap = new HashMap<>();
        courseMap.put("records", courseList);
        courseMap.put("total", count);
        return courseMap;
    }

    @Override
    public Map<String, Object> findIndexInfo() {
        Map<String,Object> indexInfo = new HashMap<>();
        // panels信息
        List<Map<String,Object>> panels = new ArrayList<>();

        // 班级
        Map<String, Object> trainClassInfo = new HashMap<>();
        trainClassInfo.put("title","班级");
        trainClassInfo.put("value", trainClassMapper.getActiveClassCount());
        trainClassInfo.put("unit","活跃");
        trainClassInfo.put("unitColor","success");
        trainClassInfo.put("subTitle","总班级");
        trainClassInfo.put("subValue",trainClassMapper.getClassCount());
        panels.add(trainClassInfo);
        // 学员
        Map<String, Object> studentInfo = new HashMap<>();
        studentInfo.put("title","学员");
        studentInfo.put("value", userMapper.getActiveStudentCount());
        studentInfo.put("unit","活跃");
        studentInfo.put("unitColor","danger");
        studentInfo.put("subTitle","总学员");
        studentInfo.put("subValue",userMapper.getStudentCount(4));
        panels.add(studentInfo);
        // 课程
        Long courseCount = courseMapper.getCourseCount();
        Map<String, Object> courseInfo = new HashMap<>();
        courseInfo.put("title","课程");
        courseInfo.put("value", courseCount);
        courseInfo.put("unit","活跃");
        courseInfo.put("unitColor","");
        courseInfo.put("subTitle","总课程");
        courseInfo.put("subValue", courseCount);
        panels.add(courseInfo);
        // 考试
        Map<String, Object> examInfo = new HashMap<>();
        examInfo.put("title","考试");
        examInfo.put("value", courseCount);
        examInfo.put("unit","活跃");
        examInfo.put("unitColor","warning");
        examInfo.put("subTitle","总考试");
        examInfo.put("subValue", courseCount);
        panels.add(examInfo);

        indexInfo.put("panels",panels);

        // periods信息
        List<Map<String,Object>> periods = new ArrayList<>();
        // 待开始
        Map<String, Object> toBegin = new HashMap<>();
        toBegin.put("label","待开始");
        toBegin.put("value",liveMapper.getInfoCount(0));
        periods.add(toBegin);
        // 直播
        Map<String, Object> liveStreaming = new HashMap<>();
        liveStreaming.put("label","直播中");
        liveStreaming.put("value",liveMapper.getInfoCount(1));
        periods.add(liveStreaming);
        // 已结束
        Map<String, Object> ended = new HashMap<>();
        ended.put("label","已结束");
        ended.put("value",liveMapper.getInfoCount(2));
        periods.add(ended);
        // 录播
        Map<String, Object> recordedBroadcast = new HashMap<>();
        recordedBroadcast.put("label","录播");
        recordedBroadcast.put("value",livePlaybackMapper.getInfoCount());
        periods.add(recordedBroadcast);

        indexInfo.put("periods",periods);

        // users信息
        List<Map<String,Object>> users = new ArrayList<>();
        // 管理员
        Map<String, Object> administrators = new HashMap<>();
        administrators.put("label","管理员");
        administrators.put("value",userMapper.getStudentCount(1));
        users.add(administrators);
        // 班主任
        Map<String, Object> headTeacher = new HashMap<>();
        headTeacher.put("label","班主任");
        headTeacher.put("value",userMapper.getStudentCount(2));
        users.add(headTeacher);
        // 教师
        Map<String, Object> teacher = new HashMap<>();
        teacher.put("label","教师");
        teacher.put("value",userMapper.getStudentCount(3));
        users.add(teacher);
        // 学员
        Map<String, Object> student = new HashMap<>();
        student.put("label","学员");
        student.put("value",userMapper.getStudentCount(4));
        users.add(student);

        indexInfo.put("users",users);

        return indexInfo;
    }
}
